<?php

class M_pages extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function add_pages($page_name, $content, $cat_name) {
//        $query = "SELECT cat_id FROM categories WHERE cat_name = '$cat_name'";
//        $result = $this->db->query($query);
        $this->db->select('cat_id');
        $result = $this->db->get_where('categories',array('cat_name'=>$cat_name));
        $r = $result->result_array();
        
        $cat_id = $r[0]['cat_id'];
//        $query = "INSERT INTO pages (user_id, cat_id, page_name,content, post_on) 
//                  values (1, '{$cat_id}', '{$page_name}', '{$content}', NOW())";
        $data = array(
            'user_id'=>1,
            'cat_id'=>$cat_id,
            'page_name'=>$page_name,
            'content'=>$content,
            'post_on'=>NOW()
        );
        
        $result = $this->db->insert('pages',$data);
        return $result;
    }

    public function view_pages() {
        //$query = "SELECT * FROM pages";
        $result = $this->db->get('pages');
        //$qa = $this->db->mysqli_num_rows($result);
     // echo $result->num_row();
        return $result->result_array();
    }
    public function view_page($page_id){
       // $q = "SELECT * FROM pages WHERE page_id = '$page_id'";
        $r = $this->db->get_where('pages',array('page_id'=>$page_id));
        return $r->result_array();
    }
    public function update_page($page_id, $page_name, $content,$cat_id){
        //echo $page_id.$page_name.$cat_id.$content;
        //khoang cach giua 2 bien trong SET phai co dau ,
        //$query = "UPDATE pages SET page_name = '{$page_name}',cat_id='{$cat_id}',content='{$content}'  WHERE page_id = '{$page_id}' ";
        $data = array(
            'page_name'=>$page_name,
            'cat_id'=>$cat_id,
            'content'=>$content
        );
        $result = $this->db->update('pages',$data,array('page_id'=>$page_id));
        return $result;
    }

    public function delete($page_id) {

        //$query = "DELETE FROM pages WHERE page_id = '$page_id' LIMIT 1";
        $result = $this->db->delete('pages',array('page_id'=>$page_id));
        return $result;
    }
    public function test_data(){
        //$this->db->
        $result = $this->db->get('categories');
       // mysql_affected_rows()
        //mysql_
     $num = $this->db->affected_rows($result);
        return $num;
    }
}

?>
